package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Loans;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class Release_EditAcknowledgement extends ActionSupport {

    private LoansDAO loan = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private List<Loans> gridModel;
    private Integer rows = 0;
    private Integer page = 0;
    private Integer total = 0;
    private Integer record = 0;
    private String sord;
    private String sidx;
    private String searchField;
    private String searchString;
    private String searchOper;
    private boolean loadonce = false;
    private Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");

    @Actions({
        @Action(value = "/getackno", results = {
            @Result(name = "success", type = "json")
        })
    })
    public String execute() {
        try {
                
                int to = (getRows() * getPage());
                int from = to - getRows();
                
                DetachedCriteria criteria = DetachedCriteria.forClass(Loans.class);
                criteria.createCriteria("loanverification", "loanverif").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                criteria.createCriteria("loanverif.borrowers", "bo").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                criteria.createCriteria("bo.branches", "branch").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

                criteria.add(Restrictions.eq("voucherprint", true));
                criteria.add(Restrictions.eq("acknowledgementprint", false));
                
                if (!b.getBranchcode().equalsIgnoreCase("HO")) {
                    criteria.add(Restrictions.eq("branch.branchid", b.getBranchid()));                    
                }
                
            
        


        setRecord(loan.countByCriteria(criteria));
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);

        if (to > getRecord()) {
            to = getRecord();
        }

        if (loadonce) {
            setGridModel(loan.findByCriteria(criteria, from, to));
        } else {
            setGridModel(loan.findByCriteria(criteria, from, to));
        }

        setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
        } catch (Exception e) {
                return ERROR;
        }
        return SUCCESS;
    }

    public String getJSON() {
        return execute();
    }

    /**
     * @return how many rows we want to have into the grid
     */
    public Integer getRows() {
        return rows;
    }

    /**
     * @param rows
     *          how many rows we want to have into the grid
     */
    public void setRows(Integer rows) {
        this.rows = rows;
    }

    /**
     * @return current page of the query
     */
    public Integer getPage() {
        return page;
    }

    /**
     * @param page
     *          current page of the query
     */
    public void setPage(Integer page) {
        this.page = page;
    }

    /**
     * @return total pages for the query
     */
    public Integer getTotal() {
        return total;
    }

    /**
     * @param total
     *          total pages for the query
     */
    public void setTotal(Integer total) {
        this.total = total;
    }

    /**
     * @return total number of records for the query. e.g. select count(*) from
     *         table
     */
    public Integer getRecord() {
        return record;
    }

    /**
     * @param record
     *          total number of records for the query. e.g. select count(*) from
     *          table
     */
    public void setRecord(Integer record) {

        this.record = record;

        if (this.record > 0 && this.rows > 0) {
            this.total = (int) Math.ceil((double) this.record / (double) this.rows);
        } else {
            this.total = 0;
        }
    }

    /**
     * @return an collection that contains the actual data
     */
    public List<Loans> getGridModel() {
        return gridModel;
    }

    /**
     * @param gridModel
     *          an collection that contains the actual data
     */
    public void setGridModel(List<Loans> gridModel) {
        this.gridModel = gridModel;
    }

    /**
     * @return sorting order
     */
    public String getSord() {
        return sord;
    }

    /**
     * @param sord
     *          sorting order
     */
    public void setSord(String sord) {
        this.sord = sord;
    }

    /**
     * @return get index row - i.e. user click to sort.
     */
    public String getSidx() {
        return sidx;
    }

    /**
     * @param sidx
     *          get index row - i.e. user click to sort.
     */
    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public void setLoadonce(boolean loadonce) {
        this.loadonce = loadonce;
    }

}
